package com.huiminpay.merchant.service.impl;

import com.alibaba.fastjson.JSON;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.merchant.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service   //spring注解
public class SmsServiceImpl implements SmsService {

    @Autowired
    RestTemplate restTemplate;
    @Value(value = "${sms.url}")
    String url;
    @Value(value = "${sms.effectiveTime}")
    String effectiveTime;

    /**
     * 发送验证码
     *
     * @param phone 手机号
     * @return验证key
     */
    @Override
    public String sendSms(String phone) throws BusinessException{
        //组装HttpEntity
        // body
        Map<String, Object> body = new HashMap<>();
        body.put("mobile", phone); //放入手机号
        //header
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Type", "application/json");

        HttpEntity httpEntity = new HttpEntity(body, httpHeaders);


        ResponseEntity<Map> responseEntity = null;
        try {
            responseEntity = restTemplate.exchange(

//                    "http://localhost:56085/sailing/generate?effectiveTime=600&name=sms",
                    url + "/generate?name=sms&effectiveTime=" + effectiveTime,
                    HttpMethod.POST,
                    httpEntity,
                    Map.class);
        } catch (RestClientException e) {
            e.printStackTrace();
//            throw new RuntimeException("验证码发送失败'!");
            throw new BusinessException(CommonErrorCode.E_100107);
        }
        log.info("响应的数据:{}", JSON.toJSONString(responseEntity));
        Map bodyMap = responseEntity.getBody();
//        System.out.println(bodyMap);

        if (bodyMap == null || bodyMap.get("result") == null) {
//            throw new RuntimeException("验证码发送失败'!");
            throw new BusinessException(CommonErrorCode.E_100107);
        }

        Map result = (Map) bodyMap.get("result");
        String key = (String) result.get("key");
        return key;
    }

    /**
     * 校验验证码
     *
     * @param verifiyKey  验证key
     * @param verifiyCode 验证码
     */
    @Override
    public void checkVerifyCode(String verifiyKey, String verifiyCode) {
        //服务地址
        //url   http://localhost:56085/sailing
        String verifyUrl = url + "/verify?name=sms&verificationCode=" + verifiyCode + "&verificationKey=" + verifiyKey;

        //调用短信服务验证
        Map bodyMap=null;
        try {
            ResponseEntity<Map> exchange = restTemplate.exchange(verifyUrl, HttpMethod.POST, HttpEntity.EMPTY, Map.class);
            bodyMap = exchange.getBody();

        } catch (RestClientException e) {
            e.printStackTrace();
//            throw new RuntimeException("验证码校验失败");
            throw new BusinessException(CommonErrorCode.E_100102);
        }

        if (bodyMap == null || bodyMap.get("result") == null || !(boolean)bodyMap.get("result")) {
//            throw new RuntimeException("验证码校验失败");
            throw new BusinessException(CommonErrorCode.E_100102);
        }


    }
}
